home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5578 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: frco.com!usenet
  2. From: Jadam@tcmail.frco.com (Jim Adam)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: what's wrong?
  5. Date: 5 Feb 1996 19:39:58 GMT
  6. Organization: Fisher Rosemount Systems
  7. Message-ID: <4f5mee$e5p@rolaids.frco.com>
  8. References: <4ejerd$r84@news.infoserve.net>
  9. NNTP-Posting-Host: primrose.frco.com
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.11
  12.  
  13. In article <4ejerd$r84@news.infoserve.net>, paveltka@unix.infoserve.net 
  14. says...
  15.  
  16.  
  17. >        c=*ptr;
  18. >        while((c>=48&&c<=57)||//while c is digit,-,+ or .
  19. >                               (c==43)||
  20. >                    (c==45)||
  21. >                    (c==46))
  22. >        {
  23. >            c= *ptr;
  24.  
  25. >>>            strcat(szC,&c);  //  RUN-TIME ERROR -- SEE BELOW
  26.  
  27. >           *(szC + strlen(szC)-1)=*"\0";
  28. >           ptr++;
  29. >        }
  30. >        strcpy(szRest,ptr-1);
  31. >        *strpbrk(szC,"XYZR")=*"\0";//to eliminate X,Y,Z at the end of 
  32. >                                                    //the szC
  33. >        dC=atof(szC);
  34.  
  35.  
  36. The following should *not* work:
  37.  
  38.    strcat( szC, &c );
  39.  
  40. Note that strcat() expects to receive a null-terminated string,
  41. not just a pointer to a lone character.  This will *occassionaly*
  42. fail mysteriously....
  43.  
  44. In this case, I'd suggest something like:
  45.  
  46.   int curPos = 0;
  47.   while( .... )
  48.   {
  49.     szC[curPos] = *ptr;
  50.     curPos++;
  51.     ptr++;
  52.   }
  53.   szC[curPos] = 0;  // NULL TERMINATE BUFFER
  54.   
  55.   //  DO REST OF PROCESSING  
  56.  
  57.  
  58. You might also want to add some error checking to the while()
  59. condition.  I.e.,
  60.  
  61.   while( ... && curPos < sizeof( szC ))
  62.  
  63. to ensure you don't accidentally overrun the small buffer.  
  64.  
  65. Hope this helps,
  66.  
  67. Jim
  68. Jadam@tcmail.frco.com
  69.  
  70.